package com.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookstore.entity.dto.OrderDTO;
import com.bookstore.entity.po.BookPO;
import com.bookstore.entity.po.UserPO;
import com.bookstore.mapper.UserMapper;
import com.bookstore.service.BookService;
import com.bookstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {
@Autowired
    BookService bookService;
    @Override
    public boolean enoughMoney(String userName, OrderDTO orderDTO, Integer num, Integer bookId) {
        UserPO userPO = getOne(new QueryWrapper<UserPO>().eq("user_name", userName));

        BookPO bookPO = bookService.getById(bookId);

        if (userPO.getMoney() >= bookPO.getPrice() * num) {
            UserPO usr = new UserPO();
            usr.setMoney(userPO.getMoney() - bookPO.getPrice() * num);
            boolean update = update(usr, new QueryWrapper<UserPO>().eq("user_name", userName));
            return update;
        }
        return false;
    }

    @Override
    public boolean enoughMoneyGroup(String userName,Float allPrice) {
        UserPO user = getOne(new QueryWrapper<UserPO>().eq("user_name", userName));
        if (user == null) {
            return false;
        }
        if (user.getMoney() >= allPrice) {
            user.setMoney(user.getMoney() - allPrice);
            update(user,new QueryWrapper<UserPO>().eq("user_name",userName));
            return true;
        }
        return false;
    }
}
